<?php

namespace app\controller\api;

use app\model\AdminStaff;
use app\model\AdminStaffIcCard;
use app\model\AdminStudentDormitory;
use app\util\BEIBEIGFUtil;
use app\util\ReturnCode;
use think\facade\Log;
use think\facade\Validate;

class Hydropower extends Base {

    /**
     * 获取水电表信息
     */
    public function getMeterData() {
        $data = input('get.');
        $validate = Validate::rule([
            'uuid|账号' => 'require'
        ]);
        if (!$validate->check($data)) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, $validate->getError());
        }
        $staff = AdminStaff::where([
            'is_delete' => 0,
            'status' => AdminStaff::AUDIT_PASS,
            'uuid' => input('get.uuid/s'),
        ])->find();
        if (!$staff) {
            return $this->buildFailed(ReturnCode::RECORD_NOT_FOUND, '账号不存在或未审核通过');
        }
        $staffIcCard = AdminStaffIcCard::where([
            'staff_id' => $staff->id,
            'is_delete' => 0
        ])->column('student_dormitory_id');
        if (isset($staffIcCard) && count($staffIcCard) == 0) {
            return $this->buildFailed(ReturnCode::RECORD_NOT_FOUND, '账号未分配宿舍，请联系管理员');
        }
        // 找出水表不为空的
        $studentDormitory = AdminStudentDormitory::where('id', 'in', $staffIcCard)->with(['dormitory'])
            ->whereNotNull('electric_no')->where('electric_no','<>','')->find();
        if (!$studentDormitory) {
            return $this->buildFailed(ReturnCode::INVALID, '宿舍信息不存在，请联系管理员');
        }
        $water = null;
        $electric = null;
        // 查电表
        if ($studentDormitory->electric_no) {
            $result = BEIBEIGFUtil::getMeterData($studentDormitory->electric_no);
            if($result && count($result) > 0){
                $electric = $result[0];
                $electric['remaining_power'] = $electric['remaining_power'].'（度）';
            }
        }
        // 查水表
        if ($studentDormitory->water_no) {
            $result = BEIBEIGFUtil::getMeterData($studentDormitory->water_no);
            if($result && count($result) > 0){
                $water = $result[0];
                $water['remaining_power'] = ($water['mfr_code'] == 'C102' ? $water['remaining_power'].'（吨）' : $water['remaining_power'].'（元）');
            }
        }
        // 水、电
        return $this->buildSuccess([$studentDormitory,$water,$electric]);
    }

    /**
     * 获取账单信息
     */
    public function getConsConsumeHistoryRecord() {
        $data = input('get.');
        $validate = Validate::rule([
            'bar_measure_id|设备ID' => 'require',
            'bar_measure_type|设备类型' => 'require',
            'date|月份' => 'dateFormat:Y-m'
        ]);
        if (!$validate->check($data)) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, $validate->getError());
        }
        if (!isset($data['date'])) {
            $data['date'] = date("Y-m");
        }
        $result = BEIBEIGFUtil::getConsConsumeHistoryRecord($data['bar_measure_id'], $data['bar_measure_type'], $data['date']);
        if($result && count($result) > 0){
            foreach ($result as $key => &$value) {
                $value['unit'] = $value['biz_type_desc'] == '电费' ? '度' : '吨';
            }
        }
        return $this->buildSuccess($result);
    }
}